<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<meta name="viewport"
				content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
	<title>定时任务</title>

	<!-- lay ui css -->
	<link th:href="@{/component/layui/css/layui.css}" rel="stylesheet">
	<!-- 自定义 css -->
	<link rel="stylesheet" th:href="@{/dimples/css/dimples.css}"/>
	<style>
		#dimples-job .layui-form-label {
			width: 60px;
		}
	</style>
</head>
<body>
<div class="layui-fluid layui-anim dimples-anim" id="dimples-job" lay-title="定时任务">
	<div class="layui-row dimples-container">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body dimples-table-full">
					<form class="layui-form layui-table-form" lay-filter="job-table-form">
						<div class="layui-row">
							<div class="layui-col-md10">
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label">Bean名称</label>
										<div class="layui-input-inline">
											<label>
												<input type="text" name="beanName" autocomplete="off" class="layui-input">
											</label>
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">方法名称</label>
										<div class="layui-input-inline">
											<label>
												<input type="text" name="methodName" class="layui-input">
											</label>
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">状态</label>
										<div class="layui-input-inline">
											<label>
												<select name="status">
													<option value=""></option>
													<option value="0">正常</option>
													<option value="1">暂停</option>
												</select>
											</label>
										</div>
									</div>
								</div>
							</div>
							<div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
									<i class="layui-icon">&#xe848;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
									<i class="layui-icon">&#xe79b;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
										 shiro:hasAnyPermissions="job:add,job:delete,job:pause,job:resume,job:run,job:export">
									<i class="layui-icon">&#xe875;</i>
								</div>
							</div>
						</div>
					</form>
					<table lay-filter="jobTable" lay-data="{id: 'jobTable'}"></table>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/html" id="job-status">
	{{#
	var status = {
	0: {title: '正常', color: 'green'},
	1: {title: '暂停', color: 'volcano'}
	}[d.status];
	}}
	<span class="layui-badge dimples-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="job-option">
	<span shiro:lacksPermission="job:update,job:delete">
        <span class="layui-badge-dot dimples-bg-orange"></span> 无权限
    </span>
	<a lay-event="run" shiro:hasPermission="job:run"><i
					class="layui-icon dimples-edit-area dimples-green">&#xe652;</i></a>
	<a lay-event="edit" shiro:hasPermission="job:update"><i
					class="layui-icon dimples-edit-area dimples-blue">&#xe7a4;</i></a>
	<a lay-event="del" shiro:hasPermission="job:delete"><i
					class="layui-icon dimples-edit-area dimples-red">&#xe7f9;</i></a>
</script>
</body>

<!-- lay ui js -->
<script type="text/javascript" th:src="@{/component/layui/layui.js}"></script>
<!-- 页面处理 -->
<script th:inline="javascript">
    var currentUser = {};
    currentUser.roleSet = [[${roles}]];
    currentUser.permissionSet = [[${permissions}]];
    console.log("用户角色：" + currentUser.roleSet);
    console.log("用户权限：" + currentUser.permissionSet);
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'form', 'table', 'pearDimples'], function () {
        var $ = layui.jquery,
            dimples = layui.pearDimples,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#dimples-job'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns;

        form.render();
        initTable();

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {
                field: sortObject.field,
                order: sortObject.type
            });
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('jobTable');
                if (name === 'add') {
                    openModelHtml('新增任务', '/web/job/add');
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        dimples.alert.warn('请勾选需要删除的任务');
                    } else {
                        dimples.modal.confirm('删除任务', '确定删除所选任务？', function () {
                            var jobIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                jobIds.push(item.jobId)
                            });
                            deleteJobs(jobIds.join('|'))
                        });
                    }
                }
                if (name === 'pause') {
                    if (!checkStatus.data.length) {
                        dimples.alert.warn('请勾选需要暂停的任务');
                    } else {
                        dimples.modal.confirm('暂停任务', '确定暂停所选任务？', function () {
                            var jobIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                jobIds.push(item.jobId)
                            });
                            dimples.post('/job/pause/' + jobIds.join('|'), null, function () {
                                dimples.alert.success('暂停任务成功');
                                $query.click();
                            })
                        });
                    }
                }
                if (name === 'resume') {
                    if (!checkStatus.data.length) {
                        dimples.alert.warn('请勾选需要恢复的任务');
                    } else {
                        dimples.modal.confirm('恢复任务', '确定恢复所选任务？', function () {
                            var jobIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                jobIds.push(item.jobId)
                            });
                            dimples.post('/job/resume/' + jobIds.join('|'), null, function () {
                                dimples.alert.success('恢复任务成功');
                                $query.click();
                            })
                        });
                    }
                }
                if (name === 'run') {
                    if (!checkStatus.data.length) {
                        dimples.alert.warn('请勾选需要执行的任务');
                    } else {
                        dimples.modal.confirm('执行任务', '确定执行所选任务？', function () {
                            var jobIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                jobIds.push(item.jobId)
                            });
                            dimples.post('/job/run/' + jobIds.join('|'), null, function () {
                                dimples.alert.success('执行任务成功');
                                $query.click();
                            })
                        });
                    }
                }
                if (name === 'export') {
                    var params = $.extend(getQueryParams(), {
                        field: sortObject.field,
                        order: sortObject.type
                    });
                    params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
                    params.pageNum = $view.find(".layui-laypage-em").next().html();
                    dimples.download('/job/excel', params, '任务表.xlsx');
                }
            },
            options: [{
                name: 'add',
                title: '新增任务',
                perms: 'job:add'
            }, {
                name: 'delete',
                title: '删除任务',
                perms: 'job:delete'
            }, {
                name: 'pause',
                title: '暂停任务',
                perms: 'job:pause'
            }, {
                name: 'resume',
                title: '恢复任务',
                perms: 'job:resume'
            }, {
                name: 'run',
                title: '执行任务',
                perms: 'job:run'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'job:export'
            }]
        });

        table.on('tool(jobTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'run') {
                dimples.modal.confirm('执行任务', '确定执行所选任务？', function () {
                    dimples.post('/job/run/' + data.jobId, null, function () {
                        dimples.alert.success('执行任务成功');
                        $query.click();
                    })
                });
            }
            if (layEvent === 'del') {
                dimples.modal.confirm('删除任务', '确定删除该任务？', function () {
                    deleteJobs(data.jobId);
                });
            }
            if (layEvent === 'edit') {
                dimples.modal.open('修改任务', 'job/job/update/' + data.jobId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#job-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        table.on('sort(jobTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        function initTable() {
            tableIns = dimples.table.init({
                elem: $view.find('table'),
                id: 'jobTable',
                url: '/job',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'jobId', title: '任务ID', width: 80},
                    {field: 'beanName', title: 'Bean名称'},
                    {field: 'methodName', title: '方法名称', maxWidth: 200},
                    {field: 'params', title: '方法参数', maxWidth: 180},
                    {field: 'cronExpression', title: 'cron表达式', maxWidth: 180},
                    {field: 'remark', title: '备注', maxWidth: 200},
                    {field: 'createTime', title: '创建时间', minWidth: 180, sort: true},
                    {title: '状态', templet: '#job-status'},
                    {title: '操作', toolbar: '#job-option', minWidth: 140}
                ]]
            });
        }

        function deleteJobs(jobIds) {
            dimples.post('/job/delete/' + jobIds, null, function () {
                dimples.alert.success('删除任务成功');
                $query.click();
            });
        }

        function getQueryParams() {
            return {
                status: $searchForm.find("select[name='status']").val(),
                beanName: $searchForm.find("input[name='beanName']").val().trim(),
                methodName: $searchForm.find("input[name='methodName']").val().trim(),
                invalidate_ie_cache: new Date()
            };
        }

        let openModelHtml = function (title, url) {
            parent.layer.open({
                type: 2,
                title: title,
                area: [$(window).width() <= 750 ? '95%' : '45%', $(window).height() <= 350 ? '95%' : '70%'],
                offset: '70px',
                shadeClose: true,
                maxmin: true,
                anim: 0,
                skin: 'layui-layer-admin-page',
                content: url
            });

        };
    })
</script>
</html>














